﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FMSpaceShooter.ScreenManager;
using FMSpaceShooter.GameData;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using FMSpaceShooter.GameObjects;

namespace FMSpaceShooter.Screens
{
    class GameplayScreen : GameScreen
    {
        GameObjectManager objectManager;

        GameTime updateInGameTime;
        GameTime drawInGameTime;
        Random rand = new Random();

        public Random Random { get { return rand; } }

        public AudioManager AudioManager
        {
            get { return ((Game1)ScreenManager.Game).AudioManager; }
        }

        public GraphicsDeviceManager GraphicsDeviceManager
        {
            get { return ((Game1)ScreenManager.Game).GDM; }
        }

        public GameObjectManager GOM
        {
            get { return objectManager; }
        }

        public GameplayScreen()
        {
            updateInGameTime = new GameTime();
            drawInGameTime = new GameTime();
        }

        public override void LoadContent()
        {
            base.LoadContent();

            objectManager = new GameObjectManager(this);

            Vector2 res = new Vector2(GraphicsDeviceManager.PreferredBackBufferWidth,
                GraphicsDeviceManager.PreferredBackBufferHeight);
            objectManager.PlayerOne = new Player1(this, "Images/Ships/redShip", res / 2, 0, 2, new Vector2(60f, 60f));

            foreach (GameScreen screen in ScreenManager.GetScreens())
            {
                if (screen != this)
                {
                    screen.ExitScreen();
                }
            }

        }

        public override void Update(GameTime gameTime2, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (!coveredByOtherScreen)
            {
                if (ScreenManager.Input.IsPauseGame())
                {
                    ScreenManager.AddScreen(new PauseMenuScreen());
                }

                float timeModifier = 1;
                if (GOM.Negotiator != null || !GOM.PlayerOne.IsInPlay)
                {
                    timeModifier = 0.1f;
                }
                
                TimeSpan elapsedTime = new TimeSpan((long)(gameTime2.ElapsedGameTime.Ticks * timeModifier));
                updateInGameTime = new GameTime(updateInGameTime.TotalGameTime + elapsedTime, elapsedTime);

                base.Update(updateInGameTime, otherScreenHasFocus, coveredByOtherScreen);
                objectManager.PlayerOne.Update(updateInGameTime);

                foreach (var go in objectManager.GameObjects)
                {
                    go.Update(updateInGameTime);
                }

                GOM.Update(updateInGameTime); 
            }
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice graphics = ScreenManager.GraphicsDevice;
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
            

            spriteBatch.Begin();
            

            spriteBatch.Draw(GOM.GetTexture("Images/gameBG"), fullscreen,
                             new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha));

            objectManager.PlayerOne.Draw(gameTime);

            spriteBatch.Draw(objectManager.MouseTexture, ScreenManager.Input.CurrentMouseVector, 
                null, Color.White, 0, Util.Tools.GetTextureVector(objectManager.MouseTexture)/2, 
                0.5f,SpriteEffects.None,0f);

            foreach (var go in objectManager.GameObjects)
            {
                go.Draw(gameTime);
            }

            GOM.HUD.Draw(gameTime);

            spriteBatch.End();
        }

        public void EndGame()
        {
            ExitScreen();
            ScreenManager.AddScreen(new BackgroundScreen());
            ScreenManager.AddScreen(new MainMenuScreen());
            ScreenManager.AddScreen(new HighscoreMenuScreen(GOM.Score));
        }

    }
}
